package com.pinda.service.impl;

import com.pinda.common.CustomIdGenerator;
import com.pinda.commonutils.Result;
import com.pinda.work.entity.PdDriverJob;
import com.pinda.work.entity.PdTaskTransport;
import com.pinda.mapper.PdTaskTransportMapper;
import com.pinda.service.IPdTaskTransportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pinda.work.entity.PdTransportOrderTask;
import com.pinda.work.enums.transporttask.TransportTaskAssignedStatus;
import com.pinda.work.enums.transporttask.TransportTaskLoadingStatus;
import com.pinda.work.enums.transporttask.TransportTaskStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 运输任务表 服务实现类
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@Service
public class PdTaskTransportServiceImpl extends ServiceImpl<PdTaskTransportMapper, PdTaskTransport> implements IPdTaskTransportService {

    @Resource
    PdTaskTransportMapper pdTaskTransportMapper;

    @Autowired
    private CustomIdGenerator idGenerator;

    @Override
    public PdTaskTransport saveTaskTransport(PdTaskTransport taskTransport) {
        taskTransport.setId(idGenerator.nextId(taskTransport) + "");
        taskTransport.setCreateTime(String.valueOf(LocalDateTime.now()));
        taskTransport.setStatus(TransportTaskStatus.PENDING.getCode());
        taskTransport.setAssignedStatus(TransportTaskAssignedStatus.TO_BE_DISTRIBUTED.getCode());
        taskTransport.setLoadingStatus(TransportTaskLoadingStatus.EMPTY.getCode());
        save(taskTransport);
        return taskTransport;
    }

    @Override
    public boolean taskTransportUpdate(PdTaskTransport pdTaskTransport) {
        int i = pdTaskTransportMapper.updateById(pdTaskTransport);
        return i != 0 ? true : false;
    }

    @Override
    public Result<PdTaskTransport> transport(String id) {
        PdTaskTransport pdTaskTransport = pdTaskTransportMapper.selectById(id);
        if (pdTaskTransport != null && pdTaskTransport.getStatus() == 2) {
            return new Result<>(true, 200, "成功", pdTaskTransport);
        }
        return new Result<>(false, 300, "无在途任务", null);
    }

    @Override
    public PdTaskTransport selectId(String taskId) {

        return pdTaskTransportMapper.selectById(taskId);
    }
}
